<?php

require_once 'Entity.php';

/**
 * Restauracja.
 *
 * @author Rafał Cupał
 * @author Przemysław Furmańczyk
 */
class Restaurant extends Entity {
    protected $name = null;
    protected $newName = null;
    protected $location = null;
    protected $newLocation = null;
    protected $chain = null;
    protected $owner = null;
    
    public function __construct($dataBase) {
        parent::__construct($dataBase, 'restaurants');
    }
    
    public function getName() {
        return $this->name;
    }
    
    public function setName($newName) {
        $this->name = $newName;
    }
    
    public function newName($newName) {
        $this->newName = $newName;
    }
    
    public function getNewName() {
        return $this->newName;
    }
    
    public function getLocation() {
        return $this->location;
    }
    
    public function setLocation($newLocation) {
        $this->location = $newLocation;
    }
    
    public function newLocation($newLocation) {
        $this->newLocation = $newLocation;
    }
    
    public function getNewLocation($newLocation) {
        return $this->newLocation;
    }
    
    public function getOwner() {
        return $this->owner;
    }
    
    public function setOwner($newOwner) {
        $this->owner = (int)$newOwner;
    }
    
    public function getChainId() {
        return $this->chain;
    }
    
    public function attachToChain($chainId) {
        $this->chain = $chainId;
    }
    
    public function getProfit() {
        $profit = 0;
        if ($this->id > 0) {
            $command = 'SELECT SUM(profit) AS sumProfit FROM orders WHERE ' .
                    'restaurant = ' . $this->id;
            $statement = $this->dataBase->query($command);
            if ($row = $statement->fetch()) {
                $profit = $row['sumProfit'];
            }
            $statement->closeCursor();
        }
        return $profit;
    }
    
    public function load($rowData) {
        $this->id = $rowData['id'];
        $this->getRowData($rowData, $this->name, 'name');
        $this->getRowData($rowData, $this->newName, 'newName');
        $this->getRowData($rowData, $this->location, 'location');
        $this->getRowData($rowData, $this->newLocation, 'newLocation');
        $this->getRowData($rowData, $this->owner, 'owner');
        $this->getRowData($rowData, $this->chain, 'chain');
    }
    
    public function save() {
        $rowData = array();
      
        $this->setRowData($rowData, $this->name, 'name');
        $this->setRowData($rowData, $this->newName, 'newName');
        $this->setRowData($rowData, $this->location, 'location');
        $this->setRowData($rowData, $this->newLocation, 'newLocation');
        $this->setRowData($rowData, $this->owner, 'owner');
        $this->setRowData($rowData, $this->chain, 'chain');
        
        if ($this->id > 0) {
            $this->dataBase->update($this->tableName, $rowData, 
                    'id = ' . $this->id);
        }
        else {
            $this->dataBase->insert($this->tableName, $rowData);
            $this->id = $this->dataBase->lastInsertId();
        }
    }
}
